package com.jc.camerascan.fragment.scan;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jc.camerascan.R;
import com.jc.camerascan.adapter.MyMenuListAdapter;
import com.jc.camerascan.core.BaseFragment;
import com.jc.camerascan.core.http.bean.CustomApiResult;
import com.jc.camerascan.core.http.callback.TipCallBack;
import com.jc.camerascan.entity.Banner;
import com.jc.camerascan.entity.HomeMenu;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.banner.widget.banner.BannerItem;
import com.xuexiang.xui.widget.banner.widget.banner.SimpleImageBanner;
import com.xuexiang.xui.widget.banner.widget.banner.base.BaseBanner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @ProjectName: PhotoRecognition
 * @Package: com.jc.camerascan.fragment.scan
 * @ClassName: ScanFragment
 * @Description: java类作用描述
 * @Author: XuChi
 * @CreateDate: 2020/7/13 9:54
 * @Version: 1.0
 */
public class ScanFragment extends BaseFragment {

    @BindView(R.id.banner)
    SimpleImageBanner banner;
    @BindView(R.id.list)
    RecyclerView list;

    //首页菜单适配器
    private MyMenuListAdapter myMenuListAdapter;
    private List<HomeMenu> MyMenuListData = new ArrayList<>();

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        return null;
    }

    /**
     * 布局的资源id
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scan;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //配置布局，默认为vertical（垂直布局），下边这句将布局改为水平布局
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        list.setLayoutManager(layoutManager);
//        list.addItemDecoration(new SpaceItemDecoration(5, 0));
//        list.addItemDecoration(new GridSpacingItemDecoration(3, DensityUtils.dp2px(getActivity(), 5), false));
        list.setNestedScrollingEnabled(false);
        list.setHasFixedSize(false);
        myMenuListAdapter = new MyMenuListAdapter();
        list.setAdapter(myMenuListAdapter);

        getBanner();
        getMenuList();
    }

    /**
     * 获取banner
     */
    private void getBanner() {
        XHttp.get("/api/Home/GetBanner")
                .execute(new CallBackProxy<CustomApiResult<List<Banner>>, List<Banner>>(new TipCallBack<List<Banner>>() {
                    @Override
                    public void onSuccess(List<Banner> response) throws Throwable {
                        if (response != null && !response.isEmpty()) {
                            List<BannerItem> bannerlist = new ArrayList<>();
                            for (Banner topBannerBean : response) {
                                BannerItem bannerItem = new BannerItem();
                                bannerItem.setImgUrl(topBannerBean.getBanner_img());
                                bannerItem.setTitle(topBannerBean.getBanner_name());
                                bannerlist.add(bannerItem);
                            }
                            banner.setSource(bannerlist);
                            banner.startScroll();
                            banner.setVisibility(View.VISIBLE);
                            banner.setOnItemClickListener(new BaseBanner.OnItemClickListener<BannerItem>() {
                                @Override
                                public void onItemClick(View view, BannerItem item, int position) {
//                                Intent intentActivityWebview1 = new Intent();
//                                intentActivityWebview1.setClass(getActivity(), WebviewActivity.class);
//                                intentActivityWebview1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                intentActivityWebview1.putExtra("url", bean.getData().getTopBanner().get(position).getBanner_href());
//                                intentActivityWebview1.putExtra("title", bean.getData().getTopBanner().get(position).getBanner_name());
//                                startActivity(intentActivityWebview1);
                                }
                            });
                        } else {
                            banner.setVisibility(View.GONE);
                        }
                    }
                }) {
                });
    }

    /**
     * 获取首页菜单
     */
    private void getMenuList() {
        XHttp.get("/api/Home/GetMenuList")
                .execute(new CallBackProxy<CustomApiResult<List<HomeMenu>>, List<HomeMenu>>(new TipCallBack<List<HomeMenu>>() {
                    @Override
                    public void onSuccess(List<HomeMenu> response) throws Throwable {
                        MyMenuListData = response;
                        myMenuListAdapter.setNewData(MyMenuListData);
                    }
                }) {
                });
    }
}

